package main

import (
	"bytes"
	"config/model"
	"errors"
	"fmt"
	"github.com/fsnotify/fsnotify"
	"log"
	"os"
	"os/exec"
	"regexp"
	"strconv"
	"utilsx/tools/fileUtils"
)

// 获取进程ID
func getPid(processName string) (int, error) {
	//通过wmic process get name,processid | findstr server.exe获取进程ID
	buf := bytes.Buffer{}
	cmd := exec.Command("wmic", "process", "get", "name,processid")
	cmd.Stdout = &buf
	cmd.Run()
	cmd2 := exec.Command("findstr", processName)
	cmd2.Stdin = &buf
	data, _ := cmd2.CombinedOutput()
	if len(data) == 0 {
		return -1, errors.New("not find")
	}
	info := string(data)
	//这里通过正则把进程id提取出来
	reg := regexp.MustCompile(`[0-9]+`)
	pid := reg.FindString(info)
	return strconv.Atoi(pid)
}

// 启动进程
func startProcess(exePath string, args []string) error {
	attr := &os.ProcAttr{
		//files指定新进程继承的活动文件对象
		//前三个分别为，标准输入、标准输出、标准错误输出
		Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
		//新进程的环境变量
		Env: os.Environ(),
	}

	p, err := os.StartProcess(exePath, args, attr)
	if err != nil {
		return err
	}
	fmt.Println(exePath, "进程启动")
	p.Wait()
	return nil
}

func main() {
	configPath := "/home/debi/IdeaProjects/EmpShop/Server/webFull/config/config.toml"
	model.Encoder(configPath)

	//循环
	//select {}
}

func Watchh() {
	configPath := "/home/debi/IdeaProjects/EmpShop/Server/webFull/config/config.toml"
	model.Encoder(configPath)

	confFilePath := "/home/debi/IdeaProjects/EmpShop/Server/webFull/config/"

	watch, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watch.Close()
	fileUtils.WatchFile(watch, confFilePath, func(ev fsnotify.Event) {
		//我们只需关心文件的修改
		if ev.Op&fsnotify.Write == fsnotify.Write {
			fmt.Println(ev.Name, "文件写入")
		}
	})
	select {}

}
